const app = getApp()
Page({
    data: {
        tokenID: '',
        moment: [],
        page: 1,
    },
    onLoad: function(options) {
        var _this = this;
        _this.setData({
            tokenID: app.data.tokenID
        })
        _this.onReachBottom();
    },
    onPullDownRefresh: function() {
        var _this = this;
        this.setData({
            page: 1
        })
        wx.showNavigationBarLoading();
        _this.gtemomentList("down")
    },
    onReachBottom: function() {
        var that = this;
        wx.showLoading({
            title: '玩命加载中',
        })
        that.gtemomentList("up")
    },
    gtemomentList: function(type) {
        var that = this;
        that.setData({
            tokenID: app.data.tokenID
        })
        wx.request({
            url: app.data.api + '/questionnaire/questionnairePage',
            data: {
                "page": that.data.page,
                "token": that.data.tokenID
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    if (type == 'up') {
                        var moment_list = that.data.moment;

                        for (var i = 0; i < res.data.data.list.length; i++) {
                            var json = {},
                                sumnum = res.data.data.list[i].usernum > 100 ? 1000 :100;
                            json.name = res.data.data.list[i].name,
                                json.createtime = res.data.data.list[i].createtime,
                                json.usernum = res.data.data.list[i].usernum
                            json.sumnum = sumnum;
                            moment_list.push(json);
                        }
                        if (res.data.data.list.length == 0) {
                            that.setData({
                                datamsg: true
                            })
                        }
                        that.setData({
                            page: that.data.page + 1,
                            moment: that.data.moment
                        })
                    } else {
                        var moment_list = [];

                        for (var i = 0; i < res.data.data.list.length; i++) {
                            var json = {};
                            json.name = res.data.data.list[i].name,
                                json.createtime = res.data.data.list[i].createtime,
                                json.usernum = res.data.data.list[i].usernum;
                            moment_list.push(json);
                        }
                        that.setData({
                            page: that.data.page + 1,
                            moment: moment_list
                        })
                        wx.stopPullDownRefresh();
                    }
                    wx.hideLoading();
                    wx.hideNavigationBarLoading();
                } else {
                    wx.hideLoading();
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        success: function(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            } else if (res.cancel) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            }
                        }
                    });
                }
            }
        })
    },
})